Skip to content
c++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
int n, m;
std::vector<int> g[N];
int fa[N], dep[N], sz[N], top[N], son[N], dfn[N], seg[N], timer;
int col[N];
void dfs1(int u, int f) {
    dep[u] = dep[f] + 1, fa[u] = f, son[u] = 0, sz[u] = 1;
    for (int v : g[u])
        if (v != f) {
            dfs1(v, u);
            sz[u] += sz[v];
            if (sz[v] > sz[son[u]]) son[u] = v;
        }
}

void dfs2(int u, int tp) {
    top[u] = tp, dfn[u] = ++timer, seg[timer] = u;
    if (son[u]) dfs2(son[u], tp);
    for (int v : g[u]) {
        if (v != son[u] && v != fa[u]) {
            dfs2(v, v);
        }
    }
}

struct Node {
    int lc, rc, cnt, tag;
} tr[N << 2];

inline void pushup(int p) {
    tr[p].lc = tr[p << 1].lc;
    tr[p].rc = tr[p << 1 | 1].rc;
    tr[p].cnt = tr[p << 1].cnt + tr[p << 1 | 1].cnt - (tr[p << 1].rc == tr[p << 1 | 1].lc);
}
inline void apply(int p, int c) {
    tr[p].lc = tr[p].rc = c;
    tr[p].cnt = 1;
    tr[p].tag = c;
}

inline void pushdown(int p, int l, int r) {
    if (tr[p].tag) {
        apply(p << 1, tr[p].tag);
        apply(p << 1 | 1, tr[p].tag);
        tr[p].tag = 0;
    }
}

void build(int p, int l, int r) {
    if (l == r) {
        tr[p].lc = tr[p].rc = col[seg[l]];
        tr[p].cnt = 1;
        return;
    }
    int mid = (r + l) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
}

void update(int p, int l, int r, int ql, int qr, int c) {
    if (ql <= l && r <= qr) {
        apply(p, c);
        return;
    }
    pushdown(p, l, r);
    int mid = (l + r) >> 1;
    if (ql <= mid) update(p << 1, l, mid, ql, qr, c);
    if (qr > mid) update(p << 1 | 1, mid + 1, r, ql, qr, c);
    pushup(p);
}
Node query(int p, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
        return tr[p];
    }
    pushdown(p, l, r);
    int mid = (l + r) >> 1;
    if (qr <= mid) return query(p << 1, l, mid, ql, qr);
    if (ql > mid) return query(p << 1 | 1, mid + 1, r, ql, qr);

    Node a = query(p << 1, l, mid, ql, qr);
    Node b = query(p << 1 | 1, mid + 1, r, ql, qr);
    Node res;
    res.lc = a.lc;
    res.rc = b.rc;
    res.cnt = (a.cnt + b.cnt) - (a.rc == b.lc);
    return res;
}

void path(int u, int v, int c) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
        update(1, 1, n, dfn[top[u]], dfn[u], c);
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) std::swap(u, v);
    update(1, 1, n, dfn[v], dfn[u], c);
}

int query_path(int u, int v) {
    int res = 0;
    int cu = -1, cv = -1;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) std::swap(u, v), std::swap(cu, cv);
        Node a = query(1, 1, n, dfn[top[u]], dfn[u]);
        res += a.cnt - (cu == a.rc);
        cu = a.lc;
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) std::swap(u, v), std::swap(cu, cv);
    Node a = query(1, 1, n, dfn[v], dfn[u]);
    res += a.cnt - (cu ==a.rc)-(cv==a.lc); 
    return res;
}

void fc() {
    std::cin >> n >> m;
    for (int i = 1; i <= n; i++) std::cin >> col[i];
    for (int i = 1; i < n; i++) {
        int u, v;
        std::cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs1(1, 0);
    dfs2(1, 1);

    build(1, 1, n);
    while (m--) {
        char op;
        std::cin >> op;
        if (op == 'Q') {
            int u, v;
            std::cin >> u >> v;
            std::cout << query_path(u, v) << "\n";
        } else {
            int u, v, c;
            std::cin >> u >> v >> c;
            path(u, v, c);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // std::cin>>t;
    while (t--) fc();
    return 0;
}